package com.aleaho.cftrack.http;

import android.text.TextUtils;

import com.aleaho.cftrack.CFTrackApplication;
import com.aleaho.cftrack.http.api.DepartmentTreeApi;
import com.aleaho.cftrack.http.api.LoginApi;
import com.aleaho.cftrack.http.api.SignApi;
import com.aleaho.cftrack.http.entity.SignEntity;
import com.aleaho.cftrack.utils.Constants;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/9/10.
 */

public class NetWorkUtil {

    private static OkHttpClient okHttpClient;

    private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
    private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();


    private static LoginApi loginApi;
    private static DepartmentTreeApi departmentTreeApi;
    private static SignApi signApi;

    public static void initOkhttp() {
        if (okHttpClient == null) {
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(httpLoggingInterceptor)
                    .addNetworkInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {

                            Request.Builder request = chain.request().newBuilder();
                            if (!TextUtils.isEmpty(CFTrackApplication.getInstance().getToken())) {
                                //header add token
                                request.addHeader("Authorization",
                                        "Token " + CFTrackApplication.getInstance().getToken());
                                return chain.proceed(request.build());
                            }
                            return chain.proceed(request.build());
                        }
                    }).build();
        }
    }


    public static LoginApi getLoginApi() {

        initOkhttp();
        if (loginApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Constants.API_URL)
                    .client(okHttpClient)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .addConverterFactory(gsonConverterFactory)
                    .build();
            loginApi = retrofit.create(LoginApi.class);
        }
        return loginApi;
    }

    public static DepartmentTreeApi getDepartmentTreeApi() {

        initOkhttp();
        if (null == departmentTreeApi) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Constants.API_URL)
                    .client(okHttpClient)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .addConverterFactory(gsonConverterFactory)
                    .build();
            departmentTreeApi = retrofit.create(DepartmentTreeApi.class);
        }

        return departmentTreeApi;
    }


    public static SignApi getSignApi() {

        initOkhttp();
        if (null == signApi) {

            Retrofit retrofit = new Retrofit.Builder()

                    .baseUrl(Constants.API_URL)
                    .client(okHttpClient)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .addConverterFactory(gsonConverterFactory)
                    .build();
            signApi = retrofit.create(SignApi.class);
        }

        return signApi;
    }


}
